package com.edgar.baselibrary.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * Created by edgar on 2015/12/22.
 */
public final class AppUtils {

    private static final String TAG = "AppUtils";
    public static PackageInfo getPackageInfo(Context context){
        String packageName = context.getPackageName();
        try {
            return context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            Log.i(TAG,"Not find package is "+packageName+":"+e.getMessage());
            return null;
        }
    }

    public static int getAppCurrentVersionCode(Context context){
        PackageInfo packageInfo = getPackageInfo(context);
        if(packageInfo != null){
            return packageInfo.versionCode;
        }
        return 0;
    }

    public static String getAppCurrentVersionName(Context context){
        PackageInfo packageInfo = getPackageInfo(context);
        if(packageInfo != null){
            return packageInfo.versionName;
        }
        return null;
    }
}